package com.springboot.controller;

import com.springboot.model.OrderForm;
import com.springboot.model.PatientInfoForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorMakePrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;

/**
 * 本控制台用于医生在今日预约界面开药单
 * 传入的预约日期book_date参数必须是2020-11-03类型，不可以是2020-11-3
 */
@Controller
public class DoctorMakePrescriptionController {
    @Autowired
    DoctorMakePrescriptionService doctorMakePrescriptionService;
    /**
     * 医生在今日预约界面给患者开药单
     */
    @CrossOrigin
    @RequestMapping(value = "/illRecordServlet")
    public @ResponseBody
    String doctorMakePrescription(@RequestParam("doctorTel") String doctorTel,
                                  @RequestParam("patientTel") String patientTel,
                                  @RequestParam("illName") String sickName,
                                  @RequestParam("illLevel") String illLevel,
                                  @RequestParam("illResult") String sickResult,
                                  @RequestParam("illCare") String sickAttention,
                                  @RequestParam("orderDate") String bookDate,
                                  @RequestParam("orderTime") String bookTime) {
        System.out.println("医生开药单");
        try{
            Integer sickLevel=Integer.valueOf(illLevel);
            UserLoginForm doctorUser=doctorMakePrescriptionService.findUserByTel(doctorTel);
            UserLoginForm patientUser=doctorMakePrescriptionService.findUserByTel(patientTel);
            int doctorId=doctorUser.getUserId();
            int patientId=patientUser.getUserId();
            OrderForm orderform=new OrderForm();
            orderform.setDoctorId(doctorId);
            orderform.setPatientId(patientId);
            java.util.Date d = null;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
            try {
                d = format.parse(bookDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            java.sql.Date date = new java.sql.Date(d.getTime());
            orderform.setBootDate(date);
            orderform.setBootTime(bookTime);
            OrderForm order=doctorMakePrescriptionService.findBookByForeign(orderform);
            int bookId=order.getBootId();
            System.out.println(bookId);
            PatientInfoForm patientInfoForm=doctorMakePrescriptionService.findPatientBybookId(bookId);
            patientInfoForm.setSickName(sickName);
            patientInfoForm.setResultCheck(sickResult);
            patientInfoForm.setSickAttention(sickAttention);
            patientInfoForm.setSickLevel(sickLevel);
            doctorMakePrescriptionService.updatePatientInfo(patientInfoForm);
            order.setProgress("已就诊");
            doctorMakePrescriptionService.updateOrderForm(order);
            return "True";
        }catch (Exception e)
        {
            return "页面错误";
        }
    }
}